/********************************************************************
	copyright (c) by tnmujoy
    @created:	9/19/2011   15:49
    @filename: 	winConsole
    @author:	Limiko Lau (limiko.lau@gmail.com)
    @brief:	    
*********************************************************************/

#ifndef GDUT_WINCONSOLE_H_
#define GDUT_WINCONSOLE_H_

#include "iConsole.hxx"
#include <Windows.h>

namespace tnmujoy
{
namespace gdut
{

class TWinConsoleImpl : public TConsoleInterface
{
public:
    TWinConsoleImpl(){};
    ~TWinConsoleImpl(){};

    ///@name override
    ///@{
    virtual std::string error() const
    {
        return errorString_;
    }
    virtual int run(bool _withScript)
    {
        if (_withScript)
            return runWithScript();
        return runWithoutScript();
    }
    virtual void free()
    {
        FreeConsole();
    }
    ///}@
protected:
private:
    int runWithoutScript();
    int runWithScript();
    std::string errorString_;
};

//
//History:
//[9/19/2011] <limiko.lau@gmail.com> created

//////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////// 
////////////////////////////////////////////////////////////////////////// 


//-------------------------------------------------------------------------
int TWinConsoleImpl::runWithoutScript()
{
    AllocConsole();
    SetConsoleTitleA("Debug Win Output");
    HWND hWnd = GetConsoleWindow();
    HMENU hMenu;
    hMenu=GetSystemMenu(hWnd,false);
    EnableMenuItem(hMenu,SC_CLOSE,MF_GRAYED|MF_BYCOMMAND);

    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    INT hCrt = _open_osfhandle((INT)hCon, _O_TEXT);
    FILE * hf = _fdopen( hCrt, "w" );
    setvbuf( hf, NULL, _IONBF, 0);
    *stdout = *hf;

    return 0;
}
//-------------------------------------------------------------------------
int TWinConsoleImpl::runWithScript()
{
     return 0;
}

}//namespace gdut
}//namespace tnmujoy

#endif //GDUT_WINCONSOLE_H_
